package com.zwl.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zwl.po.MenuPo;
import com.zwl.po.PageBean;
import com.zwl.po.RolePo;
import com.zwl.po.UserPo;
import com.zwl.service.MenuService;
import com.zwl.service.RoleService;
import com.zwl.service.impl.MenuServiceImpl;
import com.zwl.service.impl.RoleServiceImpl;
import com.zwl.utils.ConstantUtils;
import com.zwl.utils.StringUtils;

public class RoleServlet extends BaseServlet {
	private RoleService roleService=new RoleServiceImpl();
	private MenuService menuService=new MenuServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String opr=req.getParameter("opr");
		
		if("loadPrivilege".equals(opr)){
			Map<String, Object> paramMap=new HashMap<String, Object>();
			paramMap.put("isActive", 1);
			List<MenuPo> mainMenuList=menuService.findByCondition(paramMap);
			Integer roleId=StringUtils.str2Integer(req.getParameter("roleId"), false,null);
			if(roleId!=null){
				RolePo role=roleService.findbyId(roleId);
				req.setAttribute("role", role);
			}
			paramMap.put("roleId", roleId);
			List<MenuPo> setupMenuList=menuService.findByCondition(paramMap);
			if(mainMenuList!=null && mainMenuList.size()>0
					&& setupMenuList!=null && setupMenuList.size()>0){
				for(MenuPo m1:mainMenuList){
					for(MenuPo m2:setupMenuList){
						if(m1.getId().intValue()==m2.getId().intValue()){
							m1.setIsChecked(true);
						}
					}
				}
			}
			
			req.setAttribute("mainMenuList", mainMenuList);
			req.getRequestDispatcher("setPrivilegeUI.jsp").forward(req, resp);
		}else if("savePrivilege".equals(opr)){
			int rows=0;
			String msg="";
			Integer roleId=null;
			try {
				List<Integer> menuIdList=new ArrayList<Integer>();
				String[] menuIds=req.getParameterValues("resourceIdList");
				if(menuIds!=null && menuIds.length>0){
					for(String str:menuIds){
						Integer menuId=StringUtils.str2Integer(str, false, null);
						if(menuId!=null){
							menuIdList.add(menuId);
						}
					}
				}
				
				roleId=StringUtils.str2Integer(req.getParameter("roleId"), false,null);
				rows=roleService.saveRoleMenu(roleId, menuIdList);
			} catch (Exception e) {
				e.printStackTrace();
				msg="Save failed, Please try again!"+e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('Save successfully!');location.href='roleServlet?opr=loadPrivilege&roleId="+roleId+"';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+msg+"');history.back();</script>");
			}
		}else if("updateAcitve".equals(opr)){
			int rows=0;
			String msg="";
			String prefix="";
			try {
				UserPo currUser=this.getCurrUser(req.getSession());
				Timestamp currTime=new java.sql.Timestamp(System.currentTimeMillis());
	
				Integer roleId=StringUtils.str2Integer(req.getParameter("roleId"), false,null);
				Integer isActive=StringUtils.str2Integer(req.getParameter("isActive"), false,null);
				prefix=(isActive==1)?"激活":"禁用";
				if(roleId!=null){
					RolePo role=roleService.findbyId(roleId);
					role.setIsActive(isActive);
					role.setUpdatedId(currUser.getId());
					role.setUpdatedTime(currTime);
					
					
					rows=roleService.updateRole(role);
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg=prefix+"fail:"+e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(rows>0){
				out.println("<script type='text/javascript'>alert('"+prefix+"successfully');location.href='roleServlet';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('"+msg+"');history.back();</script>");
			}
			
		}else{
//			List<RolePo> roleList=roleService.findByCondition(null);
//			req.setAttribute("list", roleList);
			Integer pageIndex=StringUtils.str2Integer(req.getParameter("pageIndex"), false, 1);
			PageBean<RolePo> pageBean=roleService.findPageByCondition(pageIndex, ConstantUtils.PAGE_SIZE, null);
			req.setAttribute("pageBean", pageBean);
			req.getRequestDispatcher("role_list.jsp").forward(req, resp);
		}
	}
}
